﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using NcfDB.Model;
using NcfDB.parsing;

namespace NcfDB
{
    static class Utility
    {
        public static bool inArray(object target, object[] array)
        {
            foreach (object item in array)
                if (target == item) return true;
             return false;
        }
        public static void writeBytes(byte[] bytes, ref FileStream fs)
        {
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
        }
        public static void writeStringWithFilling(string content, int maxBytesCount, ref FileStream fs)
        {
            byte[] bufferContent = CoreDBSystem.systemEncoding.GetBytes(content), tmpBuffer;
            tmpBuffer = new byte[maxBytesCount - bufferContent.Length];
            for (int i = 0; i < tmpBuffer.Length; i++) tmpBuffer[i] = (byte)' ';
            Utility.writeBytes(bufferContent, ref fs);
            Utility.writeBytes(tmpBuffer, ref fs);
        }
        public static int readIntFromStream(ref FileStream fs)
        {
            byte[] buffer = new byte[sizeof(int)];
            fs.Read(buffer, 0, sizeof(int));
            return BitConverter.ToInt32(buffer, 0);
        }
        public static bool readBoolFromStream(ref FileStream fs)
        {
            byte[] buffer = new byte[sizeof(bool)];
            fs.Read(buffer, 0, sizeof(bool));
            return BitConverter.ToBoolean(buffer, 0);
        }
        public static string readStringFromStream(ref FileStream fs, int bytesLimit)
        {
            byte[] buffer = new byte[bytesLimit];
            fs.Read(buffer, 0, bytesLimit);
            return CoreDBSystem.systemEncoding.GetString(buffer).Trim();
        }

        /// <summary>
        /// 解析为FieldType（仅支持字符串）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static FieldType fieldTypeParse(string str)
        {
            switch (str)
            {
                case "INT":
                    return FieldType.Int;
                case "BOOL":
                    return FieldType.Bool;
                case "DOUBLE":
                    return FieldType.Double;
                case "CHAR":
                    return FieldType.Char;
                default:
                    return FieldType.Char;//错误出口
            }
        }

        /// <summary>
        /// 数据类型检查
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool fieldTypeCheck(string str, FieldType type)
        {
            int result1;
            double result2;
            bool result3;
            switch (type)
            {
                case FieldType.Char:
                    return true;
                case FieldType.Int:
                    if (int.TryParse(str,out result1)) return true;
                    else return false;
                case FieldType.Double:
                    if (double.TryParse(str, out result2)) return true;
                    else return false;
                case FieldType.Bool:
                    if (bool.TryParse(str, out result3)) return true;
                    else return false;
                default:
                    return false;
            }
        }

        public static List<List<Word>> whereConditionParse(Table table,List<Word> words, int startIndex)
        {
            List<List<Word>> whereCollection = new List<List<Word>>();
            List<Word> newWhereQuery;
            Word newWord;
            FieldProperty curFP = new FieldProperty();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                newWhereQuery = new List<Word>();
                whereCollection.Add(newWhereQuery);
            }

            for (int i = startIndex; i < words.Count - 1 ; i++)
            {
                if (words[i].type == WORD_TYPE_ENUM.IDENTIFIER)
                {
                    curFP = table.getFieldProperty(words[i].name); //获取字段
                }
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    newWord = new Word();
                    if (words[i].type == WORD_TYPE_ENUM.IDENTIFIER)//若为标识符，替换为具体数据
                    {
                        newWord.type = WORD_TYPE_ENUM.VALUE;
                        switch (curFP.Type)
                        {
                            case FieldType.Double:
                            case FieldType.Int:
                            case FieldType.Bool:
                                newWord.name = (string)table.Rows[j][words[i].name];
                                newWord.value = (string)table.Rows[j][words[i].name];
                                break;
                            case FieldType.Char:
                            default:
                                newWord.name = "'" + table.Rows[j][words[i].name] + "'";
                                newWord.value = (string)table.Rows[j][words[i].name];
                                break;
                        }
                    }
                    else
                    {
                        newWord = words[i];
                    }
                    whereCollection[j].Add(newWord);
                }
            }

            return whereCollection;
        }
    }
}
